package com.zhx.sweet.vmcreate;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.toolkit.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhanghaixuan
 * @date 2017/10/29
 */
public class AutoResourcesGenerator {


    private static String OUTPUTDIR = "/Users/zhanghaixuan/code";

    private static String AUTHOR = "zhanghaixuan";

    private static String DriverName = "com.mysql.jdbc.Driver";
    private static String Username = "root";
    private static String Password = "1234.abcd";
    private static String Url = "jdbc:mysql://10.51.108.36:3306/zhsq?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false";

    private static String ControllerClassPath = "com.yglink.community.base.BaseController";

    private static String Parent = "com.yglink.community.model.near.entity";

    private static String[] table = new String[] { "base_activity_areaid"};

    public static void main(String[] args) {
        String outputDir = OUTPUTDIR;
        final String viewOutputDir = outputDir + "/view/";
        AutoGenerator mpg = new AutoGenerator();
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(outputDir);
        gc.setFileOverride(true);
        gc.setActiveRecord(true);
        // XML 二级缓存
        gc.setEnableCache(false);
        // XML ResultMap
        gc.setBaseResultMap(true);
        // XML columList
        gc.setBaseColumnList(true);
        gc.setAuthor(AUTHOR);
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setDriverName(DriverName);
        dsc.setUsername(Username);
        dsc.setPassword(Password);
        dsc.setUrl(Url);
        mpg.setDataSource(dsc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        // strategy.setCapitalMode(true);// 全局大写命名 ORACLE 注意
        strategy.setSuperControllerClass(ControllerClassPath);
        // 表名生成策略
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        if (table != null && table.length >0) {
            strategy.setInclude(table);
        }

        mpg.setStrategy(strategy);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(Parent);
        pc.setController(Parent);
        mpg.setPackageInfo(pc);

        // 注入自定义配置，可以在 VM 中使用 cfg.abc 设置的值
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
                this.setMap(map);
            }
        };
        // 生成的模版路径，不存在时需要先新建
        File viewDir = new File(viewOutputDir);
        if (!viewDir.exists()) {
            viewDir.mkdirs();
        }
        List<FileOutConfig> focList = new ArrayList<FileOutConfig>();

        focList.add(new FileOutConfig("/templates/list.vue.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return getGeneratorViewPath(viewOutputDir, tableInfo, ".vue");
            }
        });

        focList.add(new FileOutConfig("/templates/keepAliveIndex.vue.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return getGeneratorViewPath(viewOutputDir, tableInfo, "-index.vue");
            }
        });

        focList.add(new FileOutConfig("/templates/detail.vue.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return getGeneratorViewPath(viewOutputDir, tableInfo, "-edit.vue");
            }
        });

        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);


        //生成controller相关
        mpg.execute();
    }

    /**
     * 页面生成的文件名
     */
    private static String getGeneratorViewPath(String viewOutputDir, TableInfo tableInfo, String suffixPath) {
        String name = StringUtils.firstToLowerCase(tableInfo.getEntityName());

        String[] strs = tableInfo.getName().split("_");
        String fileName = strs[1];
        String fileDir = strs[0] + "/" + fileName;
        for (int i=2;i<strs.length;i++) {
            fileName += "-" + strs[i];
        }
        String path = viewOutputDir + "/" + fileDir + "/"  + fileName + suffixPath;
        File viewDir = new File(path).getParentFile();
        if (!viewDir.exists()) {
            viewDir.mkdirs();
        }
        return path;
    }
}
